package com.meilai.project.service.business.workbench.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.constant.BossUserEnum;
import com.meilai.project.entity.business.contract.ContractApply;
import com.meilai.project.entity.business.workbench.SaleWeekCustomerTarget;
import com.meilai.project.mapper.business.contract.ContractApplyMapper;
import com.meilai.project.mapper.business.workbench.SaleWeekCustomerTargetMapper;
import com.meilai.project.service.business.workbench.SaleWeekCustomerTargetService;
import com.meilai.project.vo.business.workbench.SaleWeekCustomerTargetVO;
import com.meilai.project.vo.business.workbench.SignatureHistoryVO;
import com.meilai.project.vo.business.workbench.WeekTransactedCustomerDetailVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 销售每周预估成交客户表 服务实现类
 * </p>
 *
 * @author haiYangXu
 * @since 2022-08-16
 */
@Service
public class SaleWeekCustomerTargetServiceImpl extends ServiceImpl<SaleWeekCustomerTargetMapper, SaleWeekCustomerTarget> implements SaleWeekCustomerTargetService {

    @Autowired
    private ContractApplyMapper contractApplyMapper;

    @Override
    public List<WeekTransactedCustomerDetailVO> queryUserWeekTargetCustomerByWeekIds(List<Long> week_ids, Long user_id, Integer type, LocalDate begin_date, LocalDate end_date) {
        List<Long> boss_ids = new ArrayList<>(Arrays.asList(BossUserEnum.values())).stream().map(BossUserEnum::getId).collect(Collectors.toList());
        // 查询时间段的客户信息
        List<WeekTransactedCustomerDetailVO> weekTransactedCustomerDetailVOS = baseMapper.queryUserWeekTargetCustomerByWeekIds(week_ids, user_id, type, boss_ids);
        // 找出客户的ids
        List<Long> ids = weekTransactedCustomerDetailVOS.stream().map(WeekTransactedCustomerDetailVO::getId).distinct().collect(Collectors.toList());
        if (ids == null || (ids != null && ids.size() == 0)) {
            ids.add(-1L);
        }
        List<ContractApply> contractApplyList = contractApplyMapper.queryEffectiveContractList(begin_date, end_date, ids);
        contractApplyList = contractApplyList.stream().filter(contract -> contract.getFlow_applyer_id().equals(user_id)).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(contractApplyList)) {
            Map<Long, List<ContractApply>> customerContractMap = contractApplyList.stream().collect(Collectors.groupingBy(ContractApply::getCustomer_id));
            weekTransactedCustomerDetailVOS.forEach(item -> {
                List<LocalDate> dateList = new ArrayList<>();
                List<BigDecimal> amount_list = new ArrayList<>();
                if (customerContractMap.containsKey(item.getId())) {
                    List<ContractApply> contractList = customerContractMap.get(item.getId());
                    // 过滤掉出符合时间段的合同
                    contractList = contractList.stream().filter(contract -> (contract.getSign_date().isBefore(item.getEnd_date()) || contract.getSign_date().equals(item.getEnd_date())) && (contract.getSign_date().isAfter(item.getBegin_date()) || contract.getSign_date().equals(item.getBegin_date()))).collect(Collectors.toList());
                    contractList.forEach(contract -> {
                        dateList.add(contract.getSign_date());
                        amount_list.add(contract.getDiscounted_price());
                    });
                }
                item.setContract_time_list(dateList);
                item.setContracted_amount_list(amount_list);
            });
        }
        return weekTransactedCustomerDetailVOS;
    }

    @Override
    public List<SignatureHistoryVO> querySignatureHistoryList(Page<SignatureHistoryVO> page, Long customer_id, LocalDate begin_date, LocalDate end_date) {
        return baseMapper.querySignatureHistoryList(page, customer_id, begin_date, end_date);
    }

    @Override
    public List<SaleWeekCustomerTargetVO> querySaleWeekCustomerTargetVOList(Integer type, List<Long> week_ids, Long user_id) {
        List<Long> boss_ids = new ArrayList<>(Arrays.asList(BossUserEnum.values())).stream().map(BossUserEnum::getId).collect(Collectors.toList());
        return baseMapper.querySaleWeekCustomerTargetVOList(type, week_ids, user_id, boss_ids);
    }
}
